package com.xn.app.muying.BroadcastReceiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.xn.app.muying.R;
import com.xn.app.muying.activity.EducationDetailsActivity;
/**
 * 顶部通知栏广播
 * Created by gaoge on 2017/1/4.
 */

public class NotificationReceiver extends BroadcastReceiver {
    //管理器
    NotificationManager mNotificationManager = null;
    // 通知显示内容
    CharSequence contentText = "通知";
    //定义下拉时显示的标题
    CharSequence contentTitle = "您有一条新信息";
    //Notification.Builder
    Notification.Builder builder = null;
    //Notification
    Notification notification = null;
    //图标
    private int icon = R.mipmap.ic_launcher;
    //获取内容分类
    private String name = "";
    //获取内容id
    private String id = "";
    //获取内容
    private String content= "";
    //获取图片
    private String pic = "";

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void onReceive(Context context, Intent intent) {
        //获取内容id
        id = intent.getStringExtra("id");
        //获取标题
        contentTitle = intent.getStringExtra("title");
        //获取内容
        contentText = intent.getStringExtra("data");
        //获取图片
        pic = intent.getStringExtra("pictureUrl");
        //获取内容分类
        name= intent.getStringExtra("name");
        // 消息通知栏
        // 定义NotificationManager
        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) context
                .getSystemService(ns);
        //加载的内容
        intent = new Intent(context, EducationDetailsActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("name", name);
        intent.putExtra("title", contentTitle);
        intent.putExtra("content", contentText);
        intent.putExtra("pic", pic);
        intent.putExtra("type", 1);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // 定义通知栏展现的内容信息
        builder = new Notification.Builder(context);
        builder.setSmallIcon(icon);
        builder.setContentTitle(contentTitle);
        builder.setContentText(contentText);
        builder.setWhen(System.currentTimeMillis()); //发送时间
        builder.setDefaults(Notification.DEFAULT_ALL);
        builder.setContentIntent(contentIntent);
        notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(1, notification);
    }
}
